package com.web.startup;

import com.core.shared.CommonUtils;
import com.core.shared.encrypt.PEncryptUtil;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: lxf
 * Date: 2017/6/28
 * Time: 11:33
 * To change this template use Editor | File and Code Templates.
 */
public class PropertyEncrypt extends PropertyPlaceholderConfigurer {

    private static final List<String> encryptKey = new ArrayList<>();

    static {
        encryptKey.add("master.jdbc.url");
        encryptKey.add("master.jdbc.username");
        encryptKey.add("master.jdbc.password");
        encryptKey.add("slave.jdbc.url");
        encryptKey.add("slave.jdbc.username");
        encryptKey.add("slave.jdbc.password");
    }

    public void addEncryptKey(String key) {
        if (CommonUtils.isNotEmpty(key)) {
            encryptKey.add(key);
        }
    }

    @Override
    protected String convertProperty(String propertyName, String propertyValue) {
        if (encryptKey.contains(propertyName)) {
            propertyValue = PEncryptUtil.decrypt(propertyValue);
        }
        return propertyValue;
    }

}
